import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(),
    routes: [{
            path: '/',
            redirect: '/home'
        },
        {
            path: '/login',
            name: 'login',
            component: () =>
                import ('@/views/LoginView.vue')
        },
        {
            path: '/home',
            name: 'home',
            component: () =>
                import ('@/views/HomeView.vue'),
            children: [{
                    path: 'manage',
                    label: '账户管理',
                    meta: { name: "账户管理" },
                    component: () =>
                        import ('@/views/manage/manageView.vue'),
                    children: [{
                        path: 'managelist',
                        name: 'managelist',
                        label: '管理员列表',
                        meta: { name: "管理员列表" },
                        component: () =>
                            import ('@/views/manage/managelist.vue')
                    }]
                },
                {
                    path: 'swipermanage',
                    label: '轮播图管理',
                    meta: { name: "轮播图管理" },
                    component: () =>
                        import ('@/views/swiper/swiperView.vue'),
                    children: [{
                            path: 'swiperlist',
                            name: 'swiperlist',
                            label: '轮播图列表',
                            meta: { name: "轮播图列表" },
                            component: () =>
                                import ('@/views/swiper/swiperlist.vue')
                        },
                        {
                            path: 'swiperAdd',
                            name: 'swiperAdd',
                            label: '添加轮播图',
                            meta: { name: "添加轮播图" },
                            component: () =>
                                import ('@/views/swiper/swiperAdd.vue')
                        }
                    ]
                },
                {
                    path: 'productmanage',
                    label: '产品管理',
                    meta: { name: "产品管理" },
                    component: () =>
                        import ('@/views/product/productView.vue'),
                    children: [{
                            path: 'productlist',
                            name: 'productlist',
                            label: '产品列表',
                            meta: { name: "产品列表" },
                            component: () =>
                                import ('@/views/product/productlist.vue')
                        },
                        {
                            path: 'secondkilllist',
                            name: 'secondkilllist',
                            label: '秒杀列表',
                            meta: { name: "秒杀列表" },
                            component: () =>
                                import ('@/views/product/secondkilllist.vue')
                        },
                        {
                            path: 'recomendlist',
                            name: 'recomendlist',
                            label: '推荐列表',
                            meta: { name: "推荐列表" },
                            component: () =>
                                import ('@/views/product/recomendlist.vue')
                        },
                        {
                            path: 'filterlist',
                            name: 'filterlist',
                            label: '筛选列表',
                            meta: { name: "筛选列表" },
                            component: () =>
                                import ('@/views/product/filterlist.vue')
                        }
                    ]
                },
                {
                    path: 'echartsmanage',
                    label: '数据可视化',
                    meta: { name: "数据可视化" },
                    component: () =>
                        import ('@/views/echarts/echartsView.vue'),
                    children: [{
                            path: 'echartsshow',
                            name: 'echartsshow',
                            label: 'echarts数据展示',
                            meta: { name: "echarts数据展示" },
                            component: () =>
                                import ('@/views/echarts/echartsshow.vue')
                        },
                        {
                            path: 'echartsother',
                            name: 'echartsother',
                            label: 'echarts其他展示',
                            meta: { name: "echarts其他展示" },
                            component: () =>
                                import ('@/views/echarts/echartsother.vue')
                        }
                    ]
                },
                {
                    path: 'editor',
                    label: '编辑器',
                    meta: { name: "编辑器" },
                    component: () =>
                        import ('@/views/editor/editorView.vue'),
                    children: [{
                        path: 'editorsubmit',
                        name: 'editorsubmit',
                        label: '富文本编辑器',
                        meta: { name: "富文本编辑器" },
                        component: () =>
                            import ('@/views/editor/editorsubmit.vue')
                    }]
                },
                {
                    path: 'map',
                    label: '地图',
                    meta: { name: "地图" },
                    component: () =>
                        import ('@/views/map/mapView.vue'),
                    children: [{
                        path: 'mapbaidu',
                        name: 'mapbaidu',
                        label: '我的地图',
                        meta: { name: "我的地图" },
                        component: () =>
                            import ('@/views/map/maplist.vue')
                    }]
                }
            ]
        }
    ]
})

// 设置全局前置守卫
router.beforeEach((to, from) => {
    if (to.path != '/login') {
        if (!localStorage.getItem('token')) {
            // 表示本地没有token,获取的值为null
            ElMessage({
                message: '用户未登录,请先登录!!!',
                type: 'warning',
                duration: 2000,
            })
            return { path: '/login' }
        }
    } else {
        // 进入登录页
        if (localStorage.getItem('token')) {
            // 判断有没有token,有的话,友好提示
            ElMessage({
                message: '登录状态有效,不需要重复登录!!!',
                type: 'warning',
                duration: 2000,
            })
            return {
                path: from.path
            }
        }
    }
})

export default router